package com.rykj.api;

import android.database.sqlite.SQLiteException;

import com.google.gson.JsonParseException;
import com.rykj.util.LogX;

import java.io.IOException;

import retrofit2.HttpException;
import rx.functions.Action1;

/**
 * Description: author: zhangsan on 16/11/21 下午2:54.
 */

public class ApiFailAction implements Action1<Throwable> {

    @Override
    public void call(Throwable throwable) {
        String erroMsg = "";
        if (throwable instanceof IOException) {
            erroMsg = "请稍后重试";
        } else if (throwable instanceof HttpException) {
            erroMsg = "请稍后重试";
        } else if (throwable instanceof JsonParseException) {
            erroMsg = "json解析错误";
        } else if (throwable instanceof SQLiteException) {
            erroMsg = "数据库错误";
        } else if (throwable instanceof NullPointerException) {
            erroMsg = "数据异常，稍后重试";
        } else if (throwable instanceof IllegalArgumentException) {
            erroMsg = throwable.getMessage();
        } else {
            erroMsg = "请稍后重试";
        }
        LogX.e("retrofit", erroMsg + "\n throwable :", throwable);
        onFail(erroMsg);

    }

    public void onFail(String msg) {

    }
}
